package lib;

import java.util.ArrayList;
import java.util.List;

import dao.DictionaryDAO;
import dao.LinkDAO;
import dao.TopicDAO;

import pojo.Dictionary;
import pojo.Link;

public class LoadLinksAndMapToTopics {
	//khong xai nua
	public void AllInOne(){
		List<Link> links = null;
		List<Dictionary> dictionary = null;
		LinkDAO linkdao = new LinkDAO();
		DictionaryDAO dictionarydao = new DictionaryDAO();
		//load links
		links = linkdao.getList();
		//
		//load dictionary
		dictionary = dictionarydao.getList();
		//
		//Voi moi dong trong dictionary tuong ung voi 1 diem trong mark
		ArrayList<Integer> marks = new ArrayList<Integer>();
		//
		for(int i = 0; i < dictionary.size(); i++){
			marks.add(0);
		}
		int biggestMark;
		for(Link link : links){
			for(int i = 0; i < dictionary.size(); i++){
				marks.set(i, 0);
			}
			RankingDictionary(link.getKeywords(), dictionary, marks);
			biggestMark = marks.get(0);
			for(int i = 1; i < marks.size(); i++){
				if(marks.get(i) > biggestMark)
					biggestMark = marks.get(i);
			}
			for(int i = 0; i < dictionary.size(); i++){
				if(marks.get(i) == biggestMark)
					linkdao.finish2(link, dictionary.get(i).getTopicId(), biggestMark);
			}
		}
	}
	
	public List<Link> insertLinks(List<Link> links)
	{
		LinkDAO linkdao = new LinkDAO();
		List<Dictionary> dictionary = null;
		
		DictionaryDAO dictionarydao = new DictionaryDAO();
		//load dictionary
		dictionary = dictionarydao.getList();
		//
		//Voi moi dong trong dictionary tuong ung voi 1 diem trong mark
		ArrayList<Integer> marks = new ArrayList<Integer>();
		//
		for(int i = 0; i < dictionary.size(); i++){
			marks.add(0);
		}
		int biggestMark;
		TopicDAO topicdao= new TopicDAO();
		List<Link> l=new ArrayList<Link>();
		for(Link link : links){
			for(int i = 0; i < dictionary.size(); i++){
				marks.set(i, 0);
			}
			RankingDictionary(link.getKeywords(), dictionary, marks);
			biggestMark = marks.get(0);
			for(int i = 1; i < marks.size(); i++){
				if(marks.get(i) > biggestMark)
					biggestMark = marks.get(i);
			}
			for(int i = 0; i < dictionary.size(); i++){
				if(marks.get(i) == biggestMark){
					linkdao.finish2(link, dictionary.get(i).getTopicId(), biggestMark);
					link.setMark(biggestMark);
					
					link.setTopic(topicdao.getTopicByID(dictionary.get(i).getTopicId()));
					
					l.add(link);
					break;
				}
					
			}
		}
		
		return l;
	}
	public void RankingDictionary(String keywords, List<Dictionary> dictionary, ArrayList<Integer> marks){
		
		String temp;
		String temp1;
		String temp2;
		int temp3;
		int temp4;
		keywords = java.text.Normalizer.normalize(keywords, java.text.Normalizer.Form.NFD).replaceAll("\\p{InCombiningDiacriticalMarks}+","").toLowerCase();
		for(int i = 0; i < dictionary.size(); i++){
			temp = dictionary.get(i).getFilterKeywords();
			temp1 = dictionary.get(i).getFilterOptionalKeywords();
			if(temp != null){
				while(!temp.isEmpty()){
					temp3 = temp.lastIndexOf('#');
					if(temp3 == -1){
						temp2 = temp.trim();
						temp = "";
					}else{
						temp2 = temp.substring(temp3 + 1).trim();
						temp = temp.substring(0, temp3);
					}
					temp3 = keywords.lastIndexOf(temp2);
					temp4 = keywords.lastIndexOf('/');
					if(temp3 != -1)
						if(temp3 > temp4){
							//if(temp3 + temp2.length() == keywords.length())
							//	marks.set(i, marks.get(i) + 150);
							//else
							
							if(temp2.length() == keywords.substring(temp4 + 1).trim().length())
								marks.set(i, marks.get(i) + 150);
							else
								marks.set(i, marks.get(i) + 100);
						}							
						else{
							marks.set(i, marks.get(i) + 1);
						}
							
					
				}
			}
			if(temp1 != null){
				while(!temp1.isEmpty()){
					temp3 = temp1.lastIndexOf('#');
					if(temp3 == -1){
						temp2 = temp1.trim();
						temp1 = "";
					}else{
						temp2 = temp1.substring(temp3 + 1).trim();
						temp1 = temp1.substring(0, temp3);
					}
					if(keywords.indexOf(temp2) != -1){
						marks.set(i, marks.get(i) + 1);
					}
				}
			}
			
		}
	}
	
	
}
